package practice;

public class Test1 {
    public class ListNode {
        int val;
        ListNode next;
        ListNode() {}
        ListNode(int val) { this.val = val; }
        ListNode(int val, ListNode next) { this.val = val; this.next = next; }
    }

    class Solution {
        //         给你一个链表的头节点 head 和一个整数 val ，请你删除链表中所有满足 Node.val == val 的节点，并返回 新的头节点
        public ListNode removeElements(ListNode head, int val) {
            //1.判断链表是否为空
            if (head == null){
                return null;
            }
            //2.先删除除了 head 后面的val
            ListNode prve = head;
            ListNode cur = head.next;
            while (cur != null){
                if (cur.val == val){
                    //删除操作
                    prve.next = cur.next;
                    cur = prve.next;
                } else {
                    //prve 和 cur 往后移动
                    prve = prve.next;
                    cur = cur.next;
                }
            }
            //3.最后判断头节点是否需要删除
            if (head.val == val){
                head = head.next;
            }
            return head;
        }
    }
    //链表反转
    public ListNode reverseList(ListNode head) {
        //1.判断链表是否为空
        if (head == null){
            return null;
        }
        //2.如果链表只有一个元素
        if (head.next == null){
            return head;
        }
        //3.一般情况
        ListNode newHead = null;
        ListNode prve = null;
        ListNode cur = head;
        ListNode next = cur.next;
        while (cur != null){
            //记录cur 的下一个节点
            next = cur.next;
            //进行反转
            cur.next = prve;
            //如果next 已经为空,说明cur就已经指向的最后一个节点,让这个节点作为新的头节点
            if (next == null){
                newHead = cur;
            }
            //prve 和 cur 往后走
            prve = cur;
            cur = next;
            //cur指向最后一个节点时,让这个节点作为新的头节点
        }
        return newHead;
    }
    //链表的中间节点
        public ListNode middleNode(ListNode head) {
            //1.判断链表是否为空
            if (head == null){
                return head;
            }
            //2.求链表长度
            int size = size(head);
            //3.找到中间节点
            ListNode cur = head;
            for (int i = 0; i < size / 2; i++){
                cur = cur.next;
            }
            return cur;
        }

        public int size(ListNode head){
            int size = 0;
            for (ListNode cur = head; cur != null; cur = cur.next){
                size++;
            }
            return size;
        }
    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        //链表为空的情况
        if (list1 == null){
            return list2;
        }
        if (list2 == null){
            return list1;
        }
        ListNode cur1 = list1;
        ListNode cur2 = list2;
        ListNode newHead = new ListNode(0);
        ListNode newTali = newHead;
        while (cur1 != null && cur2 != null){
            if (cur1.val < cur2.val){
                newTali.next = cur1;
                newTali = cur1;
                cur1 = cur1.next;
            } else {
                newTali.next = cur2;
                newTali = cur2;
                cur2 = cur2.next;
            }
        }
        if (cur1 != null){
            newTali.next = cur1;
        }
        if (cur2 != null){
            newTali.next = cur2;
        }
        return newHead.next;
    }
}
